package com.collect.tanzania.zhiScene.zhiRemind.zhiCustomer.zhiMember.zhiPerformance;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.annotation.SuppressLint;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.tanzania.R;
import com.collect.tanzania.zhiAdater.zhiLevelDetail.ZHILevelDetailCustomer;
import com.collect.tanzania.zhiBase.BaseActivity;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRCustomerPerformanceEntity;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRCustomerPerformanceListEntity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISPerfermanceEntity;
import com.collect.tanzania.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.tanzania.zhiHelper.ZHIMainProcessHelper;
import com.collect.tanzania.zhiScene.zhiRemind.zhiCustomer.zhiMember.zhiPerformance.zhiSort.ZHICustomerPerformanceCompare;
import com.collect.tanzania.zhiScene.zhiWidget.zhiDialog.ZHIDialogMessage;
import com.collect.tanzania.zhiUtils.StringUtil;
import com.collect.tanzania.zhiUtils.ZHICalendarUtil;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_PERFORMANCE_CUSTOMER_OLD)
public class ZHICustomerPerformanceOldActivity extends BaseActivity<ZHICustomerPerformancePersenter> implements ZHICustomerPerformanceContract.View {

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.tv_page_title)
    TextView tv_page_title;

    @BindView(R.id.tv_cumulative_bonus_unit)
    TextView tv_cumulative_bonus_unit;

    @BindView(R.id.tv_cumulative_bonus)
    TextView tv_cumulative_bonus;

    @BindView(R.id.tv_cumulative_bonus_date)
    TextView tv_cumulative_bonus_date;
    @BindView(R.id.iv_cumulative_bonus_date)
    ImageView iv_cumulative_bonus_date;

    @BindView(R.id.tv_total_orders_num)
    TextView tv_total_orders_num;
    @BindView(R.id.tv_total_collected_num)
    TextView tv_total_collected_num;
    @BindView(R.id.tv_total_collected_rate)
    TextView tv_total_collected_rate;

    @BindView(R.id.rc_detail_list)
    RecyclerView rc_detail_list;

    @BindView(R.id.iv_sort)
    ImageView iv_sort;

    @BindView(R.id.iv_note)
    ImageView iv_note;
    @BindView(R.id.iv_filter)
    ImageView iv_filter;

    @BindView(R.id.ll_dis_date_bg)
    LinearLayout ll_dis_date_bg;

    @BindView(R.id.ll_repay_date_bg)
    LinearLayout ll_repay_date_bg;

    @BindView(R.id.ll_num_bg)
    LinearLayout ll_num_bg;

    @BindView(R.id.ll_settle_num_bg)
    LinearLayout ll_settle_num_bg;

    @BindView(R.id.ll_collected_rate_bg)
    LinearLayout ll_collected_rate_bg;

    @BindView(R.id.tv_monthy_ranking)
    TextView tv_monthy_ranking;

    @BindView(R.id.tv_daily_ranking)
    TextView tv_daily_ranking;

    private boolean mIsSortDisDateDes = true;
    private boolean mIsSortRepayDateDes = false;
    private boolean mIsSortNumDes = false;
    private boolean mIsSortSettleNumDes = false;
    private boolean mIsSortCollectedRateDes = false;

    private final ZHILevelDetailCustomer mZHILevelDetailCustomer = new ZHILevelDetailCustomer();

    private ArrayList<ZHIRCustomerPerformanceEntity> mCustomerPerformanceEntityArrayList;

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_perfermance_customer_old;
    }

    @Override
    protected void initView() {
        try {
            ZHICalendarUtil.initDateTimeOf0706L();
            ZHICalendarUtil.initDateTimeOf0801L();

            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            tv_page_title.setOnClickListener(v -> {
            });

            iv_note.setOnClickListener(v -> finish());

            iv_sort.setOnClickListener(v -> {
                try {
                    if (mCustomerPerformanceEntityArrayList == null) {
                        return;
                    }

                    if (mIsSortCollectedRateDes) {
                        mIsSortCollectedRateDes = false;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareBySettleRateASC());
                    } else {
                        mIsSortCollectedRateDes = true;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareBySettleRateDESC());
                    }

                    mZHILevelDetailCustomer.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            ll_dis_date_bg.setOnClickListener(v -> {
                try {
                    if (mCustomerPerformanceEntityArrayList == null) {
                        return;
                    }

                    if (mIsSortDisDateDes) {
                        mIsSortDisDateDes = false;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareByAssignDateASC());
                    } else {
                        mIsSortDisDateDes = true;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareByAssignDateDESC());
                    }

                    mZHILevelDetailCustomer.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            ll_repay_date_bg.setOnClickListener(v -> {
                try {
                    if (mCustomerPerformanceEntityArrayList == null) {
                        return;
                    }

                    if (mIsSortRepayDateDes) {
                        mIsSortRepayDateDes = false;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareByRepayDateASC());
                    } else {
                        mIsSortRepayDateDes = true;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareByRepayDateDESC());
                    }

                    mZHILevelDetailCustomer.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            ll_num_bg.setOnClickListener(v -> {
                try {
                    if (mCustomerPerformanceEntityArrayList == null) {
                        return;
                    }

                    if (mIsSortNumDes) {
                        mIsSortNumDes = false;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareByAssignNumASC());
                    } else {
                        mIsSortNumDes = true;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareByAssignNumDESC());
                    }

                    mZHILevelDetailCustomer.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            ll_settle_num_bg.setOnClickListener(v -> {
                try {
                    if (mCustomerPerformanceEntityArrayList == null) {
                        return;
                    }

                    if (mIsSortSettleNumDes) {
                        mIsSortSettleNumDes = false;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareBySettleNumASC());
                    } else {
                        mIsSortSettleNumDes = true;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareBySettleNumDESC());
                    }

                    mZHILevelDetailCustomer.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            ll_collected_rate_bg.setOnClickListener(v -> {
                try {
                    if (mCustomerPerformanceEntityArrayList == null) {
                        return;
                    }

                    if (mIsSortCollectedRateDes) {
                        mIsSortCollectedRateDes = false;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareBySettleRateASC());
                    } else {
                        mIsSortCollectedRateDes = true;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareBySettleRateDESC());
                    }

                    mZHILevelDetailCustomer.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            iv_filter.setOnClickListener(v -> {

            });

            iv_cumulative_bonus_date.setOnClickListener(v -> ZHIProjectRouter.toPerformanceCustomerHistory());
            tv_cumulative_bonus_date.setOnClickListener(v -> ZHIProjectRouter.toPerformanceCustomerHistory());

            initEvent();
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @Override
    public void onGetCustomerFollowDPerformance(ZHIRCustomerPerformanceListEntity zhirCustomerPerformanceListEntity, ZHISPerfermanceEntity zhisPerfermanceEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            ///////////////////////
            ArrayList<ZHIRCustomerPerformanceEntity> zhirCustomerPerformanceEntityArrayListOri = zhirCustomerPerformanceListEntity.getDataList();
            ArrayList<ZHIRCustomerPerformanceEntity> zhirCustomerPerformanceEntityArrayList = new ArrayList<>();

            for (int i = 0; i < zhirCustomerPerformanceEntityArrayListOri.size(); i++) {
                ZHIRCustomerPerformanceEntity zhirCustomerPerformanceEntity = zhirCustomerPerformanceEntityArrayListOri.get(i);
                zhirCustomerPerformanceEntity.setLimitDate(zhirCustomerPerformanceListEntity.getLimitDate());
                if (zhirCustomerPerformanceEntity.isAfter0706Date()) {
                    zhirCustomerPerformanceEntityArrayList.add(zhirCustomerPerformanceEntity);
                }
            }

            ///////////////////////
            tv_monthy_ranking.setText(zhirCustomerPerformanceListEntity.getRankData().getMonthRank());
            tv_daily_ranking.setText(zhirCustomerPerformanceListEntity.getRankData().getTodayRank());

            ///////////////////////
            tv_cumulative_bonus.setText(zhirCustomerPerformanceListEntity.getBonusFormat());

            ///////////////////////
            int totalNum = 0;
            for (int i = 0; i < zhirCustomerPerformanceEntityArrayList.size(); i++) {
                ZHIRCustomerPerformanceEntity zhirCustomerPerformanceEntity = zhirCustomerPerformanceEntityArrayList.get(i);
                if (zhirCustomerPerformanceEntity.isHighLightFlag()) {
                    totalNum += zhirCustomerPerformanceEntity.getAssignNumFormatInt();
                }
            }
            tv_total_orders_num.setText(String.valueOf(totalNum));

            ///////////////////////
            int settledNum = 0;
            for (int i = 0; i < zhirCustomerPerformanceEntityArrayList.size(); i++) {
                ZHIRCustomerPerformanceEntity zhirCustomerPerformanceEntity = zhirCustomerPerformanceEntityArrayList.get(i);
                if (zhirCustomerPerformanceEntity.isHighLightFlag()) {
                    settledNum += zhirCustomerPerformanceEntity.getRepayNumFormatInt();
                }
            }
            tv_total_collected_num.setText(String.valueOf(settledNum));

            ///////////////////////
            String collectRateStr = "0";
            if (totalNum > 0) {
                float totalNumL = totalNum * 1.0F;
                float settledNumL = settledNum * 1.0F;
                float collectRateL = settledNumL / totalNumL;
                collectRateStr = StringUtil.numberFormatterThousandPercent(collectRateL + "");
            }
            tv_total_collected_rate.setText(collectRateStr);

            ///////////////////////
            Collections.sort(zhirCustomerPerformanceEntityArrayList, new ZHICustomerPerformanceCompare.ZHICustomerPerformanceCompareByAssignDateDESC());
            mCustomerPerformanceEntityArrayList = zhirCustomerPerformanceEntityArrayList;
            mZHILevelDetailCustomer.setNewData(zhirCustomerPerformanceEntityArrayList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint({"InflateParams", "SetTextI18n"})
    public void initEvent() {
        srl_swipe_layout.setOnRefreshListener(() -> initData(false));

        mZHILevelDetailCustomer.setEmptyView(getRefreshEmptyView());

        rc_detail_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
        rc_detail_list.setAdapter(mZHILevelDetailCustomer);
    }

    public void initData(boolean showDialog) {
        ZHISPerfermanceEntity zhisPerfermanceEntity = getCaseSearchParams();
        assert mPresenter != null;
        mPresenter.getCustomerFollowDPerformance(getActivity(), zhisPerfermanceEntity, showDialog);
    }

    public void refreshData() {
        initData(true);
    }

    private ZHISPerfermanceEntity getCaseSearchParams() {
        //return mZHIRootGroupFragmentInterface.onGetPerfermanceFilterParams();

        String gStartDate = ZHICalendarUtil.getCurMonthFirstDayFormatString();
        String gEndDate = ZHICalendarUtil.getCurMonthCurDayFormatString();

        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
        zhisPerfermanceEntity.setLeven(ZHIMainProcessHelper.getInstance().getLoginEntity().getDefaultLevenApiFormat());
        zhisPerfermanceEntity.setStartDateForDisplay(gStartDate);
        zhisPerfermanceEntity.setEndDateForDisplay(gEndDate);
        zhisPerfermanceEntity.setStartDate(gStartDate);
        zhisPerfermanceEntity.setEndDate(gEndDate);
        zhisPerfermanceEntity.setLeaderUserId(ZHIMainProcessHelper.getInstance().getLoginEntity().getUserId());
        return zhisPerfermanceEntity;
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> initData(true));
        return emptyView;
    }

    private void showBonusDialog() {
        ZHIDialogMessage.showDialog(getActivity(), null, new SpannableStringBuilder(""), true, null);
    }
}